#include "testApp.h"
#include "stdio.h"

//--------------------------------------------------------------
testApp::testApp(){
}

//--------------------------------------------------------------
void testApp::setup(){
    ofSetVerticalSync(true);
	ofBackground(000, 000, 000);
    ofSetFrameRate(25);
    myFont.loadFont("Megatone.ttf", 24);
    background.loadSound("bgsound.mp3",true);
    background.setLoop(true);
    background.play();

    //------------------------------- PIEZO -----------------------------

    // Read from Arduino
	//serial.setup("/COM8", 9600); -- Turn on upon active piezo
	//serial.startContinuesRead(); -- Turn on upon active piezo
	ofAddListener(serial.NEW_MESSAGE,this,&testApp::onNewMessage);
	ofAddListener(serial.NO_DATA,this,&testApp::onNoData);

    // Building trail
	value=0;

	// Removing road if no data
    //debugReadCount=0;
    numNoData=0;
    data=false;

    // Distance counter
    afstand=0;

	// ------------------------------ 3D Road ------------------------------

	glEnable(GL_DEPTH_TEST);
	centerX = ofGetWidth()/2;
	centerY = ofGetHeight()/2;
	centerZ = 0;

	rotX = 0;
	rotY = 0;
	bSmoothLight = true;

	// Reflexions
	ofxMaterialSpecular(120, 120, 120); //how much specular light will be reflect by the surface
	ofxMaterialShininess(20); //how concentrated the reflexion will be (between 0 and 128

	//each light will emit white reflexions
	light1.specular(255, 255, 255);
	light2.specular(255, 255, 255);
	light3.specular(255, 255, 255);

	camera.position(centerX, centerY, 5000); //initialize the camera at a far position from the sphere

}

// Interpreted new message from piezo

void testApp::onNewMessage(string & message)
{

    int position = ofToInt(message); // Get piezo number

    float x;

    x= (float)position / (float)6 * (float)ofGetWidth(); // Convert Piezo data to x value

    afstand ++;

    data = true;

    targetX = x;

}

// Interpreted nodata message from piezo

void testApp::onNoData(string & message)
{
    numNoData++;

  //  if (numNoData > 805) { // Turn on upon active piezo
    //    data = false;
        numNoData=0;
    //}

}

void testApp::update(){

    if(requestRead)

	 // ------------------------------ PIEZO -------------------

	  {
		serial.sendRequest();
		requestRead = false;
	  }

	rotX += 1;
	rotY += 2;

	//light1
	float L1DirectionX = 5;
	float L1DirectionY = 0;
	float L1DirectionZ = 0;

	light1.directionalLight(255, 0, 0, L1DirectionX, L1DirectionY, L1DirectionZ);

	//light3
	float L3PosX = ofGetWidth();
	float L3PosY = 400;
	float L3PosZ = 500;
	light3.pointLight(0, 0, 255, L3PosX, L3PosY, L3PosZ);

    camera.lerpPosition(416, 518, 350, 0.1);

    // X in update
    x += (targetX-x) / 6; // Devide by 6 to get an ease,
	trail.push_back(x); // Pushback to get trail
	if (trail.size()>500) trail.erase(trail.begin()); // Remove road whech value 500 is reached

}

void testApp::draw(){

    // 3D weg Camera
    camera.place();
	ofxLightsOn();
	ofSetColor(255, 255, 255);
    ofSetColor(0x7FFF00);

     // Draw boxes
  //  if (data) { /// Turn on upon active piezo
        for (int i=0; i<trail.size(); i++) { // Draw trail with trail.size
            int z = 0-(trail.size()-i)*10; // Use Z to get depth and use trail.size to draw it into the distance
            ofxBox(trail.at(i)+70, centerY+470, z, 280, 60, 80);
    //        }

    }

    // Draw afstand
    ofSetColor(255, 155, 155);
	myFont.drawString(ofToString(afstand), 160,120);
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){
//    Testen met keyboard
//    if (key == 'a'){
//            targetX = 200;
//    }
//    if (key == 's'){
//            targetX = 400;
//    }
//    if (key == 'd'){
//            targetX = 600;
//    }
//    if (key == 'f'){
//            targetX = 800;
//    }
//    cout << "key: " << key << endl;
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
    //Testen met muis

	//targetX = x;
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

    requestRead = true;
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::resized(int w, int h){

}

